package cn.com.eado.preado.viewmodel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import cn.com.eado.crm.app.utils.LogUtil
import cn.com.eado.crm.m.db.AppIcon
import cn.com.eado.crm.m.repository.AppRepository
import cn.com.eado.crm.m.repository.LoginRepository
import cn.com.eado.crm.model.AbsentLiveData
import cn.com.eado.crm.model.Resource

/**
 *
 */
class AppViewModel : ViewModel() {
    private val repository by lazy { AppRepository() }

    private val _load = MediatorLiveData<Boolean>()  //可观察的
    private val load: LiveData<Boolean> = _load
    //http请求参数
    var name = ""

    //指定请求返回的类型为:String（本app的所有请求基本使用此方式）
    val loadResult: LiveData<Resource<List<AppIcon>>> = Transformations.switchMap(load){ load ->
        if (load){
            LogUtil.e("LoginViewModel", "loginResult: name = $name")
            repository.load(name)
        }else{
            AbsentLiveData.create()  //null
        }
    }

    fun setLoad(b: Boolean){
        _load.value = b  //赋值时，会触发loginResult的执行。true:则执行repository.login(name, pwd, mac)
    }

}